
import { safeCb } from '../core/safeCb'
import { keys } from '../core/keys'
import { isArrLike } from '../core/isArrLike'
// 11528
/**
 * 类似es6 Array的map方法
 * 对集合的每个元素调用转换函数生成与之对应的数组
 * map([4, 8], function(n) {
    return n * n;
   }); // -> [16, 64]

   dependencies safeCb keys isArrLike
 * @param {*} obj 
 * @param {*} iterator 
 * @param {*} ctx 
 * @returns 
 */
export function map (obj, iterator, ctx) {
  iterator = safeCb(iterator, ctx);

  var _keys = !isArrLike(obj) && keys(obj);

  var len = (_keys || obj).length;
  var results = Array(len);

  for (var i = 0; i < len; i++) {
    var curKey = _keys ? _keys[i] : i;
    results[i] = iterator(obj[curKey], curKey, obj);
  }

  return results;
}